<?php
/**
 * 商品首页控制器
 */
class ListController extends CommonController{
	
	//列表首页
	public function index(){
		//获取栏目id
		$cid = Q('get.cid',0,'intval');
		
		//查询热门商品
		$goodsHot = M()->JOIN("__goods__ g  JOIN __goods_info__ info ON g.gid = info.goods_gid")->where("goods_hot = '1' ")->limit(10)->all();
		
		//重组热们商品图片
		foreach($goodsHot as $k => $v){
			$imgUrl = array_filter(explode(',', $v['goods_info_smallIMg']));//将字符串转数组 并过滤掉空值
			$imgUrl = array_splice($imgUrl,0,1);
			$goodsHot[$k]['imgUrl'] = $imgUrl[0];//抽取一张做列表图
		}
		
		//卖家推荐
		$goodsdesc = M()->JOIN("__goods__ g  JOIN __goods_info__ info ON g.gid = info.goods_gid")->order('goods_click desc')->limit(4)->all();
		
		//重组热们商品图片
		foreach($goodsdesc as $k => $v){
			$imgUrl = array_filter(explode(',', $v['goods_info_smallIMg']));//将字符串转数组 并过滤掉空值
			$imgUrl = array_splice($imgUrl,0,1);
			$goodsdesc[$k]['imgUrl'] = $imgUrl[0];//抽取一张做列表图
		}
		
		//栏目分类
		$category = M('category')->where("cid = '{$cid}'")->find();
		$pid = $category['pid'];
		
		//查找父级栏目
		$category = M('category')->where("pid = '{$pid}'")->all();
		
		//循环组合父级栏目的子栏目
		foreach($category as $k =>$v ){
			//用$v的cid 查询栏目下所有pid和cid 相同的子栏目
			$cid = $v['cid'];
			$Soncategory = M('category')->where("pid = '{$cid}'")->all();
			//判断是否有子栏目 如果有就查询子栏目  没有就查询父级栏目 再组合子栏目
			if(empty($Soncategory)){
				$pid = $v['pid'];									
				$Soncategory = M('category')->where("pid = '{$pid}'")->all();
			}
			//判断返回是否存在栏目
			if($Soncategory){
				foreach($Soncategory as $kk => $vv){
					$category[$k]['son']['category_name'][$kk] = $vv['category_name'];
					$category[$k]['son']['cid'][$kk] = $vv['cid'];
				}
			}else{//有子栏目就组合数据  没有就设置空
				$category[$k] =null;
			}			
		}
		$category = array_filter($category);
		$this->assign('category',$category);
		$this->assign('goodsdesc',$goodsdesc);
		$this->assign('goodsHot',$goodsHot);
		

		/**
		 * 商品列表规格属性筛选功能
		 */
		//获取分类子集 cid
		$cid = Q('get.cid',0,'intval');
		//查询所有分类
		$data = M('category')->all();
		//调用方法查询所有子集
		$sonCid = $this->_getSonCid($data,$cid);	
		//压入当前列表页cid 获取当前栏目下的所有cid 
		$sonCid[]=$cid;
		//将数组转位字符串
		$soncidstr = implode(',', $sonCid);
		//商品表中查询所有cid在内的商品
		$cateGid = M('goods')->where("category_cid in('" .$soncidstr. "')")->getField('gid',true);
		//将商品id数组转字符串
		$cateGidStr = implode(',', $cateGid);
		//判断如果产寻返回有值 就执行 筛选
		if($cateGid){
			//通过商品gid获得属性
			$attr = M("goods_attr")->where("goods_gid in('".$cateGidStr."')")->group('goods_attr_value')->all();
			//临时数组 按属性aid分组
			$tempAttrAid = array();
			foreach($attr as $v){
				$tempAttrAid[$v['type_attr_aid']][] = $v;
			}
			//将规格与属性按分组组合插入
			foreach($tempAttrAid as $type_attr_aid =>$v){
				$tempAttr[] = array(
					'name' =>M('type_attr')->where("aid =".$type_attr_aid)->getField("attr_name"),
					'varlue' =>$v
				);
			}
			//判断栏目是否有规格属性
			if(isset($tempAttr)){
				//分配筛选规格属性
				$this->assign('tempAttr',$tempAttr); 
				//组合地址栏属性
				$num = count($tempAttr);//计算规格属性个数
				//处理地址栏规格属性值 如果没有就用0_补齐规格属性总数个
				$param = isset($_GET['param'])?explode('_', $_GET['param']):array_fill(0,$num,0);
				//分配地址栏参数
				$this->assign('param',$param);
				//$filterGids = array();
				//进行筛选
				foreach($param as $v){
					if($v){
						$filterGids[]=M()->join("__goods_attr__ g1 JOIN __goods_attr__ g2 ON g1.goods_attr_value = g2.goods_attr_value")->where("g1.type_attr_aid = '{$v}'")->getField("g2.goods_gid",true);
					}
				}
			}
	 
		
			//如果有筛选的gid
			if(isset($filterGids)){
				//取交集
				$filterGidsValue = $filterGids[0]; //初始化给第一个值 取交集
				foreach($filterGids as $v){
					$filterGidsValue = array_intersect($filterGidsValue, $v);
				}
				//取得交集gid之后，再和分类下面的商品gid再取交集
				$filterGidsValue = array_intersect($filterGidsValue, $cateGid);
				P($filterGidsValue);
			}else{
				//全部不限时就使当前分类下所有gid商品
				$filterGidsValue = $cateGid;
			}
	
		}else{
			//如果商品下面没有商品gid 就清空
			$filterGidsValue = array();
		
		}
		//分页
		$total = count($filterGidsValue);
		$this->assign('total',$total);
		$pageNum =  (isset($_GET['page']))?(int)$_GET['page']:0;
		$this->assign('pageNUm',$pageNum);
		//实例化分页  ('总条数'，'每页显示条数','显示页码数')
		$pageObj = new Page($total,4,2);
		$page = $pageObj->show();
		$this->assign('page',$page);
		$limit = $pageObj->limit();
	

		//当前页显示的最大条数
		$num = $limit*$pageNum+$limit;
		//当前页页面已显示条数
		$nowNum = count($limit);
		$this->assign('nowNum',$nowNum);
		$lim = explode(',' ,$limit);
		$lim = $lim['1'];
		$this->assign('lim',$lim);
		
		//如果筛选有返回商品id就查询商品信息 分配视图
		if(count($filterGidsValue)>=1){
			$goodsGid = implode(',', $filterGidsValue);
			//根据筛选查询商品
			$listGoods = M()->JOIN("__goods__ g  JOIN __goods_info__ info ON g.gid = info.goods_gid")->where("gid in(".$goodsGid.")")->limit($limit)->all();
			//重组图片调用路径
			foreach($listGoods as $k => $v){
				$imgUrl = array_filter(explode(',', $v['goods_info_smallIMg']));//将字符串转数组 并过滤掉空值
				$imgUrl = array_splice($imgUrl,0,1);
				$listGoods[$k]['imgUrl'] = $imgUrl[0];//抽取一张做列表图
			}
			$this->assign('listGodds',$listGoods);
		}

		//调用模板
		$this->dis('list');
	}

	
	//递归查询cid 
	public function _getSonCid($data,$cid){
		//临时变量
		$temp = array();
		//循环所有栏目 进行判断
		foreach($data as $v){
			//如果pid和cid 相等即栏目下的子栏目 压入cid
			if($v['pid']==$cid){
				$temp[] = $v['cid'];
				//合并所有cid
				$temp = array_merge($temp,$this->_getSonCid($data, $v['cid']));
			}
		}
		return $temp;
	}
}
